#include<16f877a.h>
#device ADC=10
#include<stdlib.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
//#use delay(clock=20000000)
//#use delay(clock=11592000)
#use delay(clock=16000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
float solar,battery,ldr;
#define HIGH 1
#define LOW 0
//#define solar PIN_A0
//#define battery PIN_A1
//#define ldr PIN_A2
#define RelayDrive1 PIN_C0
#define RelayDrive2 PIN_C1

 void main()
 {
   char *string1,*string2,*string3;
   int i;
   float battery_volt,solar_volt,ldr_volt;
   setup_port_A(ALL_ANALOG);
   setup_adc(ADC_CLOCK_INTERNAL);
   while(1)
   {
      set_adc_channel(0);
      delay_us(200);
      solar=Read_ADC();
      solar_volt=((solar*5)/1024)*3;

      set_adc_channel(1);
      delay_us(200);
      battery=Read_ADC();
      battery_volt=((battery*5)/1024)*3;

      set_adc_channel(2);
      delay_us(200);
      ldr=Read_ADC();
      printf("%u\n\r",ldr);
      ldr_volt=((ldr*5)/1024);

      /*sprintf(string1,"%f",solar_volt);
      
      for(i=0;string1[i]!='\0';i++)
      {
      putchar(string1[i]);
      }*/
      /*while(*string1!='\0')
      {
      putc((*string1));
      string1++;
      }*/
      printf("Battery Voltage is ");
      printf("%f\n\r",battery_volt);

      printf("Solar Panel Voltage is");
      printf("%f\n\r",solar_volt);

      printf("LDR Voltage is ");
      printf("%f\n\r",ldr_volt);

  if( ldr_Volt > 4)
  {
   printf("Too Dark, Switch ON Both Lamps");
   output_bit(RelayDrive1, HIGH);
   output_bit(RelayDrive2, HIGH);
  }
  else if (ldr_volt > 3.0)
  {
    printf("LOW Light, Switch ON one Lamp");
    output_bit(RelayDrive1, LOW);
    output_bit(RelayDrive2, HIGH);
  }
  else
  {
    printf("Sufficient Light is there, Switch OFF both Lamps");
    output_bit(RelayDrive1, LOW);
    output_bit(RelayDrive2, LOW);
  }
  delay_ms(500);
   }
}
